﻿using System;
using System.Windows.Forms;
using Microsoft.Expression.Encoder.Devices;
using System.Data.SqlClient;
using System.Data;
//using RawInput_dll;

namespace Sekolah
{
    public partial class AdminForm : Form
    {
        public AdminForm()
        {
            InitializeComponent();
        }

        private void AdminForm_Load(object sender, EventArgs e)
        {
            string videoDeviceName = AppConfig.ReadSetting(AppConfig.VIDEO_DEVICE_KEY);
            string cardReaderId = AppConfig.ReadSetting(AppConfig.CARD_READER_KEY);
            string databaseConnection = AppConfig.ReadSetting(AppConfig.DATABASE_KEY);

            var videoDeviceList = EncoderDevices.FindDevices(EncoderDeviceType.Video);
            comboBox1.DataSource = videoDeviceList;
            comboBox1.ValueMember = "Name";
            comboBox1.DisplayMember = "Name";
            comboBox1.SelectedValue = videoDeviceName;

            var usbDevices = UsbDevice.GetUSBDevices();
            comboBox2.DataSource = usbDevices;
            comboBox2.ValueMember = "DeviceID";
            comboBox2.DisplayMember = "DeviceID";
            comboBox2.SelectedValue = cardReaderId;

            textBox3.Text = databaseConnection;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedValue != null)
            {
                AppConfig.AddUpdateAppSettings(AppConfig.VIDEO_DEVICE_KEY, comboBox1.SelectedValue.ToString());
            }

            if (comboBox2.SelectedValue != null)
            {
                AppConfig.AddUpdateAppSettings(AppConfig.CARD_READER_KEY, comboBox2.SelectedValue.ToString());
            }

            AppConfig.AddUpdateAppSettings(AppConfig.DATABASE_KEY, textBox3.Text);
            Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //sp_card_cek_nomorinduk
            string sql = "exec sp_card_cek_nomorinduk '" + textBox2.Text + "', 0";
            using (var da = new SqlDataAdapter(sql, textBox3.Text))
            {
                var dt = new DataTable();
                da.Fill(dt);

                if (dt.Rows.Count > 0)
                {
                    MessageBox.Show(this, dt.Rows[0]["NAMA"].ToString());
                }
                else
                {
                    MessageBox.Show(this, "Nomor induk tidak terdaftar");
                }

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string sql = "insert into CARD_MAPPING (CARD_ID, NOMOR_INDUK, TANGGAL_MAPPING) select '" + textBox1.Text +
                         "', '" + textBox2.Text + "', GETDATE()";
            using (var cn = new SqlConnection(textBox3.Text))
            {
                cn.Open();
                SqlCommand cmd = new SqlCommand(sql, cn);
                var ra = cmd.ExecuteNonQuery();
                if (ra > 0)
                {
                    textBox1.Text = string.Empty;
                    textBox2.Text = string.Empty;
                    MessageBox.Show("Data berhasil disimpan");
                }
                cn.Close();
            }
            
        }
    }
}
